<template>
  <div>
    <div class="navs">
      <van-nav-bar
        title="交易物流"
        @click-left="onClickLeft"
        left-text="返回"
        left-arrow
      >
        <template #right> 设置 </template>
      </van-nav-bar>
    </div>
    <div class="main" ref="wrapper">
      <div>
        <div style="height: 1.5rem"></div>
        <div class="item" v-for="(item, index) in orderlist" :key="index">
          <van-cell-group>
            <van-cell :title="'乐淘购提醒您：' + item.title">
              <template #right-icon>{{ changetime(item.Time) }}</template>
            </van-cell>
          </van-cell-group>
          <div class="item_2">
            <div class="item_img">
              <img :src="item.goodsPic" alt="" />
            </div>
            <div class="item_msg">
              <p>{{ item.msg }}</p>
              <div class="bnt">
                <van-button
                  style="height: 30px"
                  @click="toDetals(item)"
                  color="#7232dd"
                  plain
                  >查看详细</van-button
                >
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
// const ws = new WebSocket("ws://localhost:8060");
const ws = new WebSocket("ws://106.55.48.147:8060");
import myfun from "../../utils/timeDate.js";
import BScroll from "@better-scroll/core";
import { mapState } from "vuex";
export default {
  data() {
    return {
      orderlist: [],
    };
  },
  mounted() {
    this.initBScroll();
    this.getLogisticsInformation();
    ws.addEventListener("open", this.handleWsOpen.bind(this), false);
    ws.addEventListener("close", this.handleWsClose.bind(this), false);
    ws.addEventListener("error", this.handleWsError.bind(this), false);
    ws.addEventListener("message", this.handleWsMessage.bind(this), false); //介绍信息对策方法
  },
  activated() {
    this.getLogisticsInformation();
  },
  computed: {
    ...mapState(["emailLogin"]),
  },
  watch: {
    orderlist() {
      this.$nextTick(() => {
        this.scroll.refresh();
      });
    },
  },
  methods: {
    onClickLeft() {
      this.$router.push("/Message");
    },
    initBScroll() {
      this.scroll = new BScroll(this.$refs.wrapper, {
        mouseWheel: true,
        click: true,
        tap: true,
      });
    },
    refresh() {
      this.scroll && this.scroll.refresh();
    },
    async toDetals(item) {
      console.log(item);
      const { data: res } = await this.$http.get("GetLocation", {
        params: { num: item.OrderNo },
      });
      if (res.status === 200) {
        let data = {
          StartingPoint: res.Start,
          EndPoint: res.End,
        };
        this.$store.commit("setJingwei", data);
        this.$store.commit("setOrdersNum", item.OrderNo);
        this.$store.commit("setOrdersPic", item.goodsPic);
      }

      this.$router.push("/LogisticsInfo");
    },
    async getLogisticsInformation() {
      const { data: res } = await this.$http.get("GetLogisticsInformation", {
        params: { id: this.emailLogin[0].userID },
      });
      console.log(res);
      this.orderlist = res.data;
    },
    changetime(t) {
      return myfun.dateTime1(t);
    },
    handleWsOpen(e) {
      console.log("连接成功：", e);
      this.getUid();
    },
    handleWsClose(e) {
      console.log("连接关闭：", e);
    },
    handleWsError(e) {
      console.log("连接错误：", e);
    },
    //接收消息
    handleWsMessage(e) {
      let data = JSON.parse(e.data);
      console.log(data);
      this.orderlist.push(data);
    },
    getUid() {
      ws.send(JSON.stringify({ uid: this.emailLogin[0].userID }));
    },
  },
};
</script>

<style lang="less" scoped>
.main {
  position: absolute;
  overflow: hidden;
  width: 100%;
  top: 20px;
  left: 0;
  right: 0;
  bottom: 0;
  background: #f7f7f7;
  .item {
    width: 350px;
    margin: 10px auto;
    background: white;
    .item_2 {
      padding: 10px;
      display: flex;

      .item_img {
        width: 70px;
        height: 70px;
      }
      .item_msg {
        margin-left: 10px;
        p {
          display: -webkit-box;
          overflow: hidden;
          -webkit-box-orient: vertical;
          -webkit-line-clamp: 1;
          text-overflow: ellipsis;
          width: 250px;
          color: #b5b5b5;
          font-size: 15px;
        }
        .bnt {
          margin-top: 20px;
        }
      }
    }
  }
}
</style>